package com.nervenets.template.controller.setting;

import com.nervenets.general.aspect.SysLog;
import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.aspect.JwtRole;
import com.nervenets.general.jwt.aspect.JwtSecurity;
import com.nervenets.general.redis.RedisSyncLocker;
import com.nervenets.general.web.BaseEntityController;
import com.nervenets.general.web.params.BoolIdParams;
import com.nervenets.template.controller.setting.params.GlobalRuleFormParams;
import com.nervenets.template.controller.setting.params.GlobalRulePagingParams;
import com.nervenets.template.hibernate.domain.common.GlobalRule;
import com.nervenets.template.hibernate.domain.common.GlobalRuleSetting;
import com.nervenets.template.hibernate.domain.common.vo.GlobalRuleFiled;
import com.nervenets.template.service.GlobalRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Api(value = "全局设置接口v1", tags = "全局设置接口")
@RestController
@RequestMapping("/api/globalRule")
@JwtRole(group = "system", groupName = "系统管理", function = "globalRule", functionName = "全局设置")
public class GlobalRuleController extends BaseEntityController<GlobalRule, GlobalRuleService, GlobalRuleFormParams, GlobalRulePagingParams, GlobalRule, GlobalRule> {
    @Resource
    private RedisSyncLocker<ResponseEntity<ResponseResult<GlobalRule>>> redisSyncLocker;

    @SysLog
    @ApiOperation(value = "启用或禁用")
    @PostMapping("/enable")
    @JwtSecurity(permission = "enable", permissionName = "禁用启用")
    public ResponseEntity<ResponseResult<GlobalRule>> enable(@Valid @RequestBody BoolIdParams params) throws Exception {
        return redisSyncLocker.idLock(params.getTargetId(), () -> {
            GlobalRule globalRule = service.findOne(params.getTargetId());
            service.updateGlobalRuleToTrue(globalRule);
            return successMessage(globalRule);
        });
    }

    @ApiOperation(value = "获取表单字段")
    @PostMapping("/fields")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<List<GlobalRuleFiled>>> formFields() {
        List<GlobalRuleFiled> ruleFields = new ArrayList<GlobalRuleFiled>();
        final Field[] fields = GlobalRuleSetting.class.getDeclaredFields();
        for (Field field : fields) {
            final String fieldName = field.getName();
            String description = null;
            String fieldTypeName = null;
            String group = null;
            String def = null;
            if (field.isAnnotationPresent(ApiModelProperty.class)) {
                final ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
                description = property.value();
                fieldTypeName = property.dataType();
                group = property.reference();
                def = property.notes();
            }

            ruleFields.add(GlobalRuleFiled.builder()
                    .key(fieldName)
                    .detail(description)
                    .type(fieldTypeName)
                    .onOff(field.getType().getSimpleName().equalsIgnoreCase("GlobalRuleEnableForm"))
                    .group(group)
                    .def(def)
                    .build());
        }
        return successMessage(ruleFields);
    }

    @ApiOperation(value = "获取当前应用的全局配置")
    @PostMapping("/get")
    @JwtSecurity(required = false)
    public ResponseEntity<ResponseResult<GlobalRule>> get() {
        return successMessage(service.getGlobalRule());
    }
}
